<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * Necessary includes.
 */
Zend_Loader::loadClass('Nordic_Util');
Zend_Loader::loadClass('Nordic_Schema_Interface');

/**
 * This class parses some types of data and returns an associatve
 * array. Also an array can be decoded and written to a file.
 * 
 * @category   Nordic
 * @package    Nordic_Schema
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: Schema.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
final class Nordic_Schema
{
	/**
	 * Here the schema-objects are saved.
	 * 
	 * @access private
	 * @var Array
	 */
	private static $_schema = array();
	
	/**
	 * This method decodes an array.
	 * 
	 * It uses the factory-method to create the right schema-object.
	 * The created object encodes the array to a string, which is
	 * returned.
	 * 
	 * @static
	 * @param String $schema schema the array should be converted to
	 * @param Array $array array that should be decoded
	 * @return String
	 */
	public static function decode ($schema, $array)
	{
		return self::factory($schema)->decode($array);
	}
	
	/**
	 * This method writes a decoded array to a file.
	 * 
	 * It uses the decode-method for decoding the array. The result
	 * of the decode-method is written to the file given in the first
	 * parameter. A boolean is returned. true indicates that the
	 * decoded array was written successfully. Otherwise false is
	 * returned.
	 * 
	 * @static
	 * @param String $path path to write the decoded array
	 * @param Array $array array to decode
	 * @return Boolean
	 */
	public static function decodeFile ($path, $array)
	{
		return (bool)Nordic_Util::write(
			$path, 
			self::decode(
				Nordic_Util::getExtension($path),
				$array
			)
		);
	}
	
	/**
	 * This method encodes a string.
	 * 
	 * It uses the factory-method to create the right schema-object.
	 * The created schema-object encodes the string to an array.
	 * 
	 * @static
	 * @param String $schema schema of the string
	 * @param String $string string to encode
	 * @return Array
	 */
	public static function encode ($schema, $string)
	{
		return self::factory($schema)->encode($string);
	}
	
	/**
	 * This method encodes a file.
	 * 
	 * The method determines the type of the file by the
	 * file-extension and uses then the the encode method for
	 * encoding the content of that file.
	 * 
	 * @static
	 * @param String $path path to the file to parse
	 * @return Array
	 */
	public static function encodeFile ($path)
	{
		return self::encode(
			Nordic_Util::getExtension($path),
			file_get_contents($path)
		);
	}
	
	/**
	 * This method returns the schema-object of the given schema.
	 * 
	 * The name of the schema is equal to the extension of a file of
	 * that schema except that the first letter is capitalized.
	 * 
	 * @static
	 * @param String $schema Schema of the to returned object
	 * @return Schema_Interface
	 */
	public static function factory ($schema)
	{
		$classname = __CLASS__ . '_' . ucfirst($schema);
		
		Zend_Loader::loadClass($classname);
		
		if(isset(self::$_schema[$classname]))
		{
			return self::$_schema[$classname];
		}
		
		$result = new $classname();
		self::$_schema[$classname] = $result;
		
		return $result;
	}
}
?>